#!/usr/bin/env python3

import os
import sys

logs = []

class Logs:
    cmd = " "
    infile = ""
    instNum = 0
    branchNum = 0
    branchMiss = 0
    def __init__(self, cmd = "", infile = ""):
        self.cmd = cmd
        self.infile = infile

    def show(self):
        print("cmd: " + self.cmd + ", " + self.infile)
        print("instructions: ", self.instNum)
        print("branches: ", self.branchNum)
        print("branch-misses: %d %.3f%% (bm/bnum) %.3f%% (bm/insts) " % (self.branchMiss, 100*self.branchMiss/self.branchNum, 100*self.branchMiss/self.instNum))

#----------------------------------------------------------------------------------#
def readfile(filename):
    file = open(filename) 
    strs=[]
    while 1:
        line = file.readline()
        if not line:
            break
        strs.append(line)
    file.close()
    return strs

#----------------------------------------------------------------------------------#
def writefile(filename, strs):
    # print("output file: "+filename)
    f1 = open(filename, 'w')
    idx = 0
    while idx < len(strs):
        f1.write(strs[idx])
        idx = idx + 1
    f1.close()


#----------------------------------------------------------------------------------#
def process(strs):
    idx = 0
    while idx < len(strs):
        hasLogs = "Performance counter stats" in strs[idx]
        #print(strs[idx])
        if hasLogs:
            words = strs[idx].split("\'")[1].split(" ")
            cmd = words[1]
            infile = os.path.split(words[2])[1]
            logtemp = Logs(cmd, infile)
            t = idx + 1
            while t < idx + 5:
                if  "instructions" in strs[t]:
                    value = int(strs[t].split("instructions")[0].replace(",","").replace(" ",""))
                    logtemp.instNum = value
                if  "branches" in strs[t] and not "branch-misses" in strs[t]:
                    value = int(strs[t].split("branches")[0].replace(",","").replace(" ",""))
                    logtemp.branchNum = value
                if "branch-misses" in strs[t]:
                    value = int(strs[t].split("branch")[0].replace(",","").replace(" ",""))
                    logtemp.branchMiss = value
                t = t+1
            idx = idx + 4
            # logtemp.show()
            # print("")
            logs.append(logtemp)
        idx = idx + 1

#----------------------------------------------------------------------------------#
def sortElement(elem):
    return elem.instNum

def sortElement1(elem):
    return elem.infile

#----------------------------------------------------------------------------------#
def toFormat():
    idx = 0
    cmds = set((""))
    while idx < len(logs):
        cmds.add(logs[idx].cmd)
        idx = idx + 1
        
    logs.sort(key=sortElement)
    logs.sort(key=sortElement1)
    for cmd in cmds:
        idx = 0
        print(cmd)
        print(", , instNum, branchNum, branchMissNum, bm/insts, bm/bnum")
        while idx < len(logs):
            if cmd == logs[idx].cmd:
                print("%s, %s, \t %d, \t %d, \t %d, \t %.3f%%, \t %.3f%%" % (logs[idx].cmd, logs[idx].infile, logs[idx].instNum, logs[idx].branchNum, logs[idx].branchMiss, 100*logs[idx].branchMiss/logs[idx].branchNum, 100*logs[idx].branchMiss/logs[idx].instNum))
            idx = idx + 1


#----------------------------------------------------------------------------------#
if len(sys.argv) < 2: 
    print("parameters are not enough.\n ./process.py logfile")
    exit()

if not os.path.exists(sys.argv[1]):
    print(sys.argv[1]+" is not exist.")
    exit()

strs = readfile(sys.argv[1])
process(strs)
toFormat()